package edu.iyte.ceng431.hw2.pos.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

import edu.iyte.ceng431.hw2.pos.model.Cash;
import edu.iyte.ceng431.hw2.pos.model.CreditCard;
import edu.iyte.ceng431.hw2.pos.model.Payment;
import edu.iyte.ceng431.hw2.pos.model.Return;
import edu.iyte.ceng431.hw2.pos.model.Sale;
import edu.iyte.ceng431.hw2.pos.model.SaleLineItem;

public class Insert {

    public static boolean insertSale(Sale sale) {
        Connection connection = ConnectDB.getConnection();
        try {
            java.sql.Timestamp date = new java.sql.Timestamp(sale.getDate().getTime());
            String exec = "insert into Sale (date,cashierId,registerId) values (?,?,?)";
            //"+sale.getId()+","+date.toString()+","+sale.getCashier().getId()+","+sale.getRegister().getId()+")" ;

            PreparedStatement pStatement = connection.prepareStatement(exec);
            pStatement.setTimestamp(1, date);
            pStatement.setInt(2, sale.getCashier().getId());
            pStatement.setInt(3, sale.getRegister().getId());
            pStatement.executeUpdate();
            ResultSet rs = pStatement.getGeneratedKeys();
            rs.next();int saleId = rs.getInt(1);
            ArrayList<SaleLineItem> saleLines = sale.getSaleLineItems();
            Iterator<SaleLineItem> it = saleLines.iterator();
            while (it.hasNext()) {
                SaleLineItem item = it.next();
                pStatement.executeUpdate("insert into SaleLine (quantity,unitPrice,productId,saleId) values (" +
                        +item.getQuantity() + "," + item.getUnitPrice() + "," + item.getProduct().getId() + "," + saleId + ")");
            }
            ArrayList<Payment> payments = sale.getPayments();
            Iterator<Payment> it2 = payments.iterator();
            while (it2.hasNext()) {
                Payment pay = it2.next();
                int form;
                String creditCard = null;
                if (pay instanceof Cash) {
                    form = 1;
                } else {
                    form = 2;
                    creditCard = ((CreditCard) pay).getCardId();
                }
                pStatement.executeUpdate("insert into Payment (amount,form,creditCard,saleId) values (" +
                        pay.getAmount() + "," + form + ",'" + creditCard + "'," + sale.getId() + ")");
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }

        return true;
    }

    public static boolean insertReturn(Return ret) {
        Connection connection = ConnectDB.getConnection();
        java.sql.Timestamp date = new java.sql.Timestamp(ret.getDate().getTime());
        String exec = "insert into returnItem (date,quantity,saleLineId) values (?,?,?)";
        try {
            PreparedStatement pStatement = connection.prepareStatement(exec);
            pStatement.setTimestamp(1, date);
            pStatement.setInt(2, ret.getQuantity());
            pStatement.setInt(3, ret.getSaleLlineItemId());
            pStatement.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
